﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;
using NetTopologySuite.Geometries.Utilities;

namespace NetTopologySuite.Operation.Union
{
	public class PointGeometryUnion
	{
		public PointGeometryUnion(IPuntal pointGeom, IGeometry otherGeom)
		{
			this._pointGeom = (IGeometry)pointGeom;
			this._otherGeom = otherGeom;
			this._geomFact = otherGeom.Factory;
		}

		public static IGeometry Union(IPuntal pointGeom, IGeometry otherGeom)
		{
			return new PointGeometryUnion(pointGeom, otherGeom).Union();
		}

		public IGeometry Union()
		{
			PointLocator pointLocator = new PointLocator();
			HashSet<Coordinate> hashSet = new HashSet<Coordinate>();
			foreach (IGeometry geometry in PointExtracter.GetPoints(this._pointGeom))
			{
				Coordinate coordinate = ((IPoint)geometry).Coordinate;
				if (pointLocator.Locate(coordinate, this._otherGeom) == Location.Exterior)
				{
					hashSet.Add(coordinate);
				}
			}
			if (hashSet.Count == 0)
			{
				return this._otherGeom;
			}
			Coordinate[] array = new Coordinate[hashSet.Count];
			hashSet.CopyTo(array, 0);
			Array.Sort<Coordinate>(array);
			ICoordinateSequence coordinateSequence = this._geomFact.CoordinateSequenceFactory.Create(array);
			IGeometry g;
			if (coordinateSequence.Count != 1)
			{
				IGeometry geometry2 = this._geomFact.CreateMultiPoint(coordinateSequence);
				g = geometry2;
			}
			else
			{
				IGeometry geometry2 = this._geomFact.CreatePoint(coordinateSequence.GetCoordinate(0));
				g = geometry2;
			}
			return GeometryCombiner.Combine(g, this._otherGeom);
		}

		private readonly IGeometryFactory _geomFact;

		private readonly IGeometry _otherGeom;

		private readonly IGeometry _pointGeom;
	}
}
